package org.jeecg.modules.jxc.homepage.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.jxc.homepage.config.PageResult;
import org.jeecg.modules.jxc.homepage.entity.JxcGoods;
import org.jeecg.modules.jxc.homepage.service.IJxcGoodsService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: jxc_goods
 * @Author: jeecg-boot
 * @Date:   2021-12-20
 * @Version: V1.0
 */
@Api(tags="jxc_goods")
@RestController
@RequestMapping("/homepage/jxcGoods")
@Slf4j
public class JxcGoodsController extends JeecgController<JxcGoods, IJxcGoodsService> {
	@Autowired
	private IJxcGoodsService jxcGoodsService;


	@PostMapping("/listAlarm")
	public List<Map<String,Object>> listAlarm(){
		Map<String, Object> map = new HashMap<>();
		List<Map<String,Object>> rs = jxcGoodsService.queryGoods();
		return rs;
	}
	/**
	 * 分页列表查询
	 *
	 * @param jxcGoods
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "jxc_goods-分页列表查询")
	@ApiOperation(value="jxc_goods-分页列表查询", notes="jxc_goods-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(JxcGoods jxcGoods,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<JxcGoods> queryWrapper = QueryGenerator.initQueryWrapper(jxcGoods, req.getParameterMap());
		Page<JxcGoods> page = new Page<JxcGoods>(pageNo, pageSize);
		IPage<JxcGoods> pageList = jxcGoodsService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

	 /**
	  * 首页分页列表查询
	  *
	  * @param goodsTypeId
	  * @param goodsNameOrCode
	  * @param pageNo
	  * @param pageSize
	  * @return
	  */
	@GetMapping("/homePageList")
	public Result<?> newPageList(Integer goodsTypeId,String goodsNameOrCode,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize){
		PageResult<JxcGoods> pageResult = jxcGoodsService.homePageList(goodsTypeId, goodsNameOrCode);
		return Result.ok(pageResult);

	}
	/**
	 *   添加
	 *
	 * @param jxcGoods
	 * @return
	 */
	@AutoLog(value = "jxc_goods-添加")
	@ApiOperation(value="jxc_goods-添加", notes="jxc_goods-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody JxcGoods jxcGoods) {
		jxcGoodsService.save(jxcGoods);
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param jxcGoods
	 * @return
	 */
	@AutoLog(value = "jxc_goods-编辑")
	@ApiOperation(value="jxc_goods-编辑", notes="jxc_goods-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody JxcGoods jxcGoods) {
		jxcGoodsService.updateById(jxcGoods);
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "jxc_goods-通过id删除")
	@ApiOperation(value="jxc_goods-通过id删除", notes="jxc_goods-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		jxcGoodsService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "jxc_goods-批量删除")
	@ApiOperation(value="jxc_goods-批量删除", notes="jxc_goods-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.jxcGoodsService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "jxc_goods-通过id查询")
	@ApiOperation(value="jxc_goods-通过id查询", notes="jxc_goods-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		JxcGoods jxcGoods = jxcGoodsService.getById(id);
		if(jxcGoods==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(jxcGoods);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param jxcGoods
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, JxcGoods jxcGoods) {
        return super.exportXls(request, jxcGoods, JxcGoods.class, "jxc_goods");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, JxcGoods.class);
    }

}
